package com.sunday.pinxinghui.model.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.model.mine.WithDrawActivity;
import com.sunday.pinxinghui.network.ApiClient;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/10.
 */

public class PurseActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.balance)
    TextView balance;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.reward)
    TextView reward;

    private long memberId;
    Member member;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purse);
        ButterKnife.bind(this);
        titleView.setText("我的钱包");
        rightTxt.setText("账单");
        memberId = BaseApplication.getInstance().getMember().getId();
    }

    private void getMember() {
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().getMember(memberId);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                     member = response.body().getResult();
                    balance.setText(String.format("%s", member.getAmount().setScale(2, RoundingMode.HALF_UP)));
                    reward.setText(String.format("%s", member.getReward().setScale(2, RoundingMode.HALF_UP)));
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }

    @OnClick({R.id.rightTxt, R.id.menu_recharge, R.id.menu_pay_setting,R.id.btn_withdraw})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rightTxt:
                intent = new Intent(mContext, BillListActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_recharge:
                intent = new Intent(mContext, RechargeActivity.class);
                intent.putExtra("balance", balance.getText().toString());
                startActivity(intent);
                break;
            case R.id.menu_pay_setting:
                intent = new Intent(mContext, PaySettingActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_withdraw:
                intent = new Intent(mContext, WithDrawActivity.class);
                intent.putExtra("reward",member.getReward().setScale(2,RoundingMode.HALF_UP).doubleValue());
                startActivity(intent);
                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        getMember();
    }
}
